package cn.zyq.ykb.service.user.impl;

import cn.zyq.ykb.beans.user.User;
import cn.zyq.ykb.system.common.BaseService;
import cn.zyq.ykb.system.exception.BusinessException;
import cn.zyq.ykb.system.ry.util.StringUtils;
import com.github.pagehelper.PageHelper;
import cn.zyq.ykb.system.ry.util.SecurityUtils;
import cn.zyq.ykb.mapper.user.PosUserMapper;
import cn.zyq.ykb.beans.user.PosUser;
import cn.zyq.ykb.service.user.PosUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.zyq.ykb.system.common.PageParam;
import com.github.pagehelper.PageInfo;


import java.time.LocalDateTime;
import java.util.UUID;
import java.util.List;

/**
 * pos用户Service业务层处理
 *
 * @author SystemAuth
 * @date 2024-03-06
 */
@Service
public class PosUserServiceImpl  extends BaseService implements PosUserService {

    @Autowired
    private PosUserMapper posUserMapper;

    /**
     * 查询pos用户
     *
     * @param posUserID pos用户主键
     * @return pos用户
     */
    @Override
    public PosUser selectPosUserByPosUserID(Integer posUserID) {
        PosUser posUser = posUserMapper.selectByPrimaryKey(posUserID);
        if (posUser.getDeleted() == 1) {
            return null;
        }
        return posUser;
    }

    /**
     * 查询pos用户
     *
     * @param serviceID 业务ID
     * @return pos用户
     */
    @Override
    public PosUser selectPosUserByServiceID(String serviceID) {
        PosUser param = new PosUser();
        param.setDeleted(0);
        param.setServiceID(serviceID);
        return this.posUserMapper.selectOne(param);
    }

    /**
     * 查询pos用户列表
     *
     * @param pageParam
     * @return pos用户
     */
    @Override
    public PageInfo<PosUser> selectPosUserList(PageParam pageParam) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        pageParam.getKv().put("deleted", 0);
        List<PosUser> list = this.posUserMapper.list(pageParam.getKv());
        return new PageInfo<>(list);
    }

    /**
     * 查询pos用户列表
     *
     * @param pageParam
     * @return pos用户
     */
    @Override
    public List<PosUser> selectPosUserAllList(PageParam pageParam) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        pageParam.getKv().put("deleted", 0);
        return this.posUserMapper.list(pageParam.getKv());
    }
    /**
     * 新增pos用户
     *
     * @param param pos用户
     * @return 结果
     */
    @Override
    public int insertPosUser(PosUser param) {
        if (StringUtils.isEmpty(param.getName()) ||
                StringUtils.isEmpty(param.getPhone())||
                StringUtils.isEmpty(param.getAddress())){
            throw new BusinessException("请填写完整参数");
        }
        this.setCreateBaseData(param, new User());
        return this.posUserMapper.insertSelective(param);
    }

    /**
     * 修改pos用户
     *
     * @param param pos用户
     * @return 结果
     */
    @Override
    public int updatePosUser(PosUser param) {
        this.setUpdateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.posUserMapper.updateByPrimaryKeySelective(param);
    }

    /**
     * 批量删除pos用户
     *
     * @param posUserIDs 需要删除的pos用户主键
     * @return 结果
     */
    @Override
    public int deletePosUserByPosUserIDs(Integer[] posUserIDs) {
        int i = 0;
        for (Integer posUserID : posUserIDs) {
            i += this.deletePosUserByPosUserID(posUserID);
        }
        return i;
    }

    /**
     * 删除pos用户信息
     *
     * @param posUserID pos用户主键
     * @return 结果
     */
    @Override
    public int deletePosUserByPosUserID(Integer posUserID) {
        PosUser posUser = this.selectPosUserByPosUserID(posUserID);
        if (posUser == null) {
            return 0;
        } else {
            PosUser storeInfo = new PosUser();
            storeInfo.setPosUserID(posUserID);
            storeInfo.setDeleted(1);
            storeInfo.setUpdateTime(LocalDateTime.now());
            this.posUserMapper.updateByPrimaryKeySelective(storeInfo);
            return 1;
        }
    }
}
